package panel.guide
{
	import assists.ButtonUtils;
	import assists.HtmlText;
	import assists.Lang;
	import assists.MoveLog;
	import assists.TextFieldUtils;
	import assists.URI;
	import assists.log.Log;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import inerface.IGuidePanel;
	
	import main.ClientConstants;
	
	import panel.home.downBtns.DownBtnsGroup;
	
	import struct.CityStruct;
	
	public class GuidePanel extends Sprite implements IGuidePanel
	{	
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		
		private var backBtn:ButtonUtils;
		
		private var _onReturn:Function;
		
		private var currentChooseBtn:int = 0;
		
		private var weatherEnterBtn:Sprite;
		
		private var down:Sprite;
		private var up:Sprite;
		
		private var moveLog:MoveLog;
		
		private var titleText:TextField;
		private var weatherText:TextField;
		
		private var upHeight:int = 0;
		private var downHeight:int = 0;
		
		private var cityChooseBtn:Sprite;
		
		private var btnImg:Bitmap;
		
		private var btnText:TextField;
		
		private var showCityList:Function;
		private var showWeather:Function;
		
		public function GuidePanel() 
		{
			
		}
		
		public function init(stage:Stage,loader:ResourceLoader,lang:Lang,uri:URI = null):void
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			upHeight = _stage.stageHeight * ClientConstants.HOME_UP_PER;
			downHeight = _stage.stageHeight * ClientConstants.HOME_DOWN_PER;
		}
		
		public override function get width():Number
		{
			return _stage.stageWidth;
		}
		
		public override function get height():Number
		{
			return _stage.stageHeight;
		}
		
		public function render(...args):void
		{
			down = new Sprite;                                                                                                  //下方工具条
			down.graphics.beginFill(0x000000);
			down.graphics.drawRect(0,0,_stage.stageWidth,downHeight);
			down.graphics.endFill();
			down.x = 0;
			down.y = _stage.stageHeight - downHeight;
			this.addChild(down);
			
			
			up = new Sprite;                                                                                                     //上方工具条
			up.graphics.beginFill(0x000000);
			up.graphics.drawRect(0,0,_stage.stageWidth,upHeight);
			up.graphics.endFill();
			up.x = 0;
			up.y = 0;
			this.addChild(up);
			
			var backImg:DisplayObject = new _loader.uiManager.backBtnIcon() as DisplayObject;
			backBtn = new ButtonUtils(backImg,formerPage);
			backBtn.scaleX = backBtn.scaleY = 0.8;
			backBtn.x = up.width * .04;
			backBtn.y = upHeight * .2;
			up.addChild(backBtn);
			backBtn.visible = false;
			
			var downBtnsGroup:DownBtnsGroup = new DownBtnsGroup(_stage);
			downBtnsGroup.init(args);
			down.addChild(downBtnsGroup);
			
			moveLog = new MoveLog(_stage);
			moveLog.x = 0;
			moveLog.y = up.height;
			up.addChild(moveLog);
			
			titleText = new TextField;
			titleText.mouseEnabled = false;
			titleText.autoSize = TextFieldAutoSize.CENTER;
			titleText.multiline = false;
			titleText.wordWrap = false;
			titleText.text = "";
			
			up.addChild(titleText);
			
			weatherEnterBtn = new Sprite;
			
			var smallImg:Bitmap = new _loader.uiManager.btnBack as Bitmap;
			smallImg.height = _stage.stageHeight * .06;
			smallImg.width = _stage.stageWidth * .2;
			
			weatherEnterBtn.addChild(smallImg);
			up.addChild(weatherEnterBtn);
			
			weatherEnterBtn.x = _stage.stageWidth * .75;
			weatherEnterBtn.y = _stage.stageHeight * .01;
			
			weatherEnterBtn.addEventListener(MouseEvent.CLICK,openWeatherHandler);
			
			weatherText = new TextField;
			weatherText.selectable = false;
			weatherText.multiline = false;
			weatherText.wordWrap = false;
			weatherText.text = "";
			
			weatherEnterBtn.addChild(weatherText);
			
			cityChooseBtn = new Sprite;
			
			btnImg = new _loader.uiManager.cityChooseBtn as Bitmap;
			btnImg.smoothing = true;
			btnImg.height = _stage.stageHeight * .06;
			cityChooseBtn.addChild(btnImg);
			
			
			btnText = new TextField;
			btnText.selectable = false;
			btnText.wordWrap = false;
			btnText.multiline = false;
			TextFieldUtils.setHtmlText(btnText,HtmlText.formatSizeColor("<b>点击选择城市</b>",0x000000,18));
			
			cityChooseBtn.addChild(btnText);
			
			resizeBtn();
			
			cityChooseBtn.y = _stage.stageHeight * .01;
			
			this.addChild(cityChooseBtn);
			
			cityChooseBtn.buttonMode = true;
			cityChooseBtn.addEventListener(MouseEvent.CLICK,showListHandler);
			
			
		}
		
		protected function openWeatherHandler(event:MouseEvent):void
		{
			if(showWeather is Function){
				showWeather();
			}
		}
		
		protected function showListHandler(event:MouseEvent):void
		{
			if(showCityList is Function){
				showCityList();
			}
		}
		
		private function resizeBtn():void
		{
			btnText.width = btnText.textWidth + 5;
			btnText.height = btnText.textHeight + 5;
			
			btnImg.width = btnText.width + 10;
			
			btnText.x = 5;
			btnText.y = _stage.stageHeight * .015;
			
			cityChooseBtn.x = (_stage.stageWidth - cityChooseBtn.width) / 2;
		}
		
		public function set showReturn(bool:Boolean):void
		{
			backBtn.visible = bool;
		}
		
		private function formerPage():void
		{
			if(_onReturn is Function){
				_onReturn();
			}
		}
		
		public function set logInfo(str:String):void
		{
			moveLog.textInfo = str;
		}
		
		public function set titelInfo(str:String):void
		{
			TextFieldUtils.setHtmlText(titleText,str);
			
			titleText.width = titleText.textWidth + 1;
			titleText.x = (_stage.stageWidth - titleText.width) / 2;
			
			titleText.height = titleText.textHeight + 1;
			titleText.y = (upHeight - titleText.height) / 2;
		}
		
		public function set weatherInfo(str:String):void
		{
			TextFieldUtils.setHtmlText(weatherText,str);
			
			weatherText.width = weatherText.textWidth + 5;
			weatherText.x = (weatherEnterBtn.width - weatherText.width)/2;
			
			weatherText.height = weatherText.textHeight + 5;
			weatherText.y = (weatherEnterBtn.height - weatherText.height)/2;
		}
		
		public function clear():void
		{
			
		}
		
		public function set currentCityInfo(city:CityStruct):void
		{
			if(city){
				TextFieldUtils.setHtmlText(btnText,HtmlText.formatSizeColor("<b>" + city.cityName + "</b.",0x000000,18));
				resizeBtn();
			}else{
				TextFieldUtils.setHtmlText(btnText,HtmlText.formatSizeColor("<b>点击选择城市</b>",0x000000,18));
				resizeBtn();
			}
		}
		
		public function set showCityChoose(bool:Boolean):void
		{
			if(bool == true){
				showChoose();
			}else{
				hideChoose();
			}
		}
		
		private function hideChoose():void
		{
			titleText.visible = true;
			cityChooseBtn.visible = false;
		}
		
		private function showChoose():void
		{
			titleText.visible = false;
			cityChooseBtn.visible = true;
		}
		
		public function set onReturn(func:Function):void { _onReturn = func; }
		
		public function set onShowList(func:Function):void { showCityList = func; }
		
		public function set onShowWeather(func:Function):void { showWeather = func; }
		
		public function  showUpAndDown(upVisible:Boolean,downVisible:Boolean):void
		{
			up.visible = upVisible;
			down.visible = downVisible;
		}
		
	}
}